package com.fu.edu.manager.controller;

import com.fu.edu.manager.domain.Student;
import com.fu.edu.manager.service.StudentService;

import java.util.Scanner;

/**
 * @author 付大大
 * @create 2021-01-06 10:53
 */
public class StudentController {
    private StudentService studentService = new StudentService();
    private Scanner scanner = new Scanner(System.in);

    //开启学生管理系统，并且展示学生管理系统菜单
    public void start() {
        Loop:while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = scanner.next();
            switch (choice) {
                case "1":
                    System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3":
                    System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("退出");
                    break Loop;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }

    }

    public void updateStudent() {
        String id = inputStudentId();
        Student student = inputStudentInfo(id);
        studentService.updateStudent(student, id);
        System.out.println("修改成功");

    }

    private void deleteStudentById() {
        String id = inputStudentId();
        studentService.deleteById(id);
        System.out.println("删除成功");

    }

    public void findAllStudent() {
        Student[] students = studentService.findAllStudent();
        if (students == null) {
            System.out.println("查无次信息，请添加后重试");
            return;
        }

        System.out.println("学号\t\t姓名\t年龄\t生日");

        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null) {
                System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t\t" + student.getBirthday());
            }
        }

    }

    public  void addStudent() {
        String id;
        while (true) {
            System.out.println("请输入学生id");
            id = scanner.next();
            boolean flag = studentService.isExists(id);

            if (flag) {
                System.out.println("学号已经被占用，请重新输入");
            } else {
                break;
            }
        }
        Student student = inputStudentInfo(id);
        boolean result = studentService.addStudent(student);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }


    }

    public String inputStudentId() {
        String id;
        while (true) {
            System.out.println("请输入学生id");
            id = scanner.next();
            boolean exists = studentService.isExists(id);
            if (!exists) {
                System.out.println("输入的id有问题，请重新输入");
            } else {
                break;
            }
        }
        return id;
    }
    public Student inputStudentInfo(String id) {
        System.out.println("请输入学生姓名");
        String name = scanner.next();
        System.out.println("请输入学生年龄");
        String age = scanner.next();
        System.out.println("请输入学生生日");
        String birthday = scanner.next();

        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setAge(age);
        student.setBirthday(birthday);
        return student;
    }

}
